package com.qms.qep.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.qms.common.annotation.Log;
import com.qms.common.core.controller.BaseController;
import com.qms.common.core.domain.AjaxResult;
import com.qms.common.core.page.TableDataInfo;
import com.qms.common.enums.BusinessType;
import com.qms.qep.domain.Certificate;
import com.qms.qep.domain.CertificateData;
import com.qms.qep.service.ICertificateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 证书管理 控制器
 */
@Api(tags = "证书管理")
@RestController
@RequestMapping("/qep/certificate")
public class CertificateController extends BaseController {
    
    @Autowired
    private ICertificateService certificateService;
    
    /**
     * 获取证书列表
     */
    @ApiOperation("获取证书列表")
    @PreAuthorize("@ss.hasPermi('qep:certificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(Certificate certificate) {
        startPage();
        List<Certificate> list = certificateService.selectCertificateList(certificate);
        return getDataTable(list);
    }
    
    /**
     * 获取证书详细信息
     */
    @ApiOperation("获取证书详细信息")
    @PreAuthorize("@ss.hasPermi('qep:certificate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(certificateService.selectCertificateById(id));
    }
    
    /**
     * 新增证书
     */
    @ApiOperation("新增证书")
    @PreAuthorize("@ss.hasPermi('qep:certificate:add')")
    @Log(title = "证书管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Certificate certificate) {
        return toAjax(certificateService.insertCertificate(certificate));
    }
    
    /**
     * 修改证书
     */
    @ApiOperation("修改证书")
    @PreAuthorize("@ss.hasPermi('qep:certificate:edit')")
    @Log(title = "证书管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Certificate certificate) {
        return toAjax(certificateService.updateCertificate(certificate));
    }
    
    /**
     * 删除证书
     */
    @ApiOperation("删除证书")
    @PreAuthorize("@ss.hasPermi('qep:certificate:remove')")
    @Log(title = "证书管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(certificateService.deleteCertificateByIds(ids));
    }
    
    /**
     * 下载证书
     */
    @ApiOperation("下载证书")
    @PreAuthorize("@ss.hasPermi('qep:certificate:export')")
    @GetMapping("/download/{id}")
    public AjaxResult download(@PathVariable("id") Long id) {
        return certificateService.downloadCertificate(id);
    }
    
    /**
     * 签发证书
     */
    @ApiOperation("签发证书")
    @PreAuthorize("@ss.hasPermi('qep:certificate:edit')")
    @Log(title = "证书管理", businessType = BusinessType.UPDATE)
    @PostMapping("/issue/{id}")
    public AjaxResult issue(@PathVariable("id") Long id, @RequestBody Certificate certificate) {
        return toAjax(certificateService.issueCertificate(id, certificate));
    }
    
    /**
     * 作废证书
     */
    @ApiOperation("作废证书")
    @PreAuthorize("@ss.hasPermi('qep:certificate:edit')")
    @Log(title = "证书管理", businessType = BusinessType.UPDATE)
    @PostMapping("/void/{id}")
    public AjaxResult voidCertificate(@PathVariable("id") Long id, @RequestBody Certificate certificate) {
        return toAjax(certificateService.voidCertificate(id, certificate.getVoidReason()));
    }
    
    /**
     * 生成证书
     */
    @ApiOperation("生成证书")
    @PreAuthorize("@ss.hasPermi('qep:certificate:add')")
    @Log(title = "证书管理", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public AjaxResult generate(@RequestBody Certificate certificate) {
        return certificateService.generateCertificate(certificate);
    }
    
    /**
     * 获取证书数据
     */
    @ApiOperation("获取证书数据")
    @PreAuthorize("@ss.hasPermi('qep:certificate:query')")
    @GetMapping("/data/{certificateId}")
    public AjaxResult getCertificateData(@PathVariable("certificateId") Long certificateId) {
        List<CertificateData> data = certificateService.selectCertificateDataByCertificateId(certificateId);
        return success(data);
    }
    
    /**
     * 保存证书数据
     */
    @ApiOperation("保存证书数据")
    @PreAuthorize("@ss.hasPermi('qep:certificate:edit')")
    @Log(title = "证书数据", businessType = BusinessType.UPDATE)
    @PostMapping("/data")
    public AjaxResult saveCertificateData(@RequestBody List<CertificateData> dataList) {
        return toAjax(certificateService.saveCertificateData(dataList));
    }
} 